#ifndef DE2924C4_073E_48DC_974A_BC468B152882
#define DE2924C4_073E_48DC_974A_BC468B152882

#include "equation.hpp"
#include "method.hpp"

static double chords_function(Equation const &eq, double a, double b,
                              double accuracy) {
  while (true) {
    double x =
        a - eq.get_first_derivative()(a) /
                (eq.get_first_derivative()(a) - eq.get_first_derivative()(b)) *
                (a - b);
    double dx = eq.get_first_derivative()(x);
    if (std::abs(dx) <= accuracy) {
      return x;
    }

    if (dx > 0) {
      b = x;
    } else {
      a = x;
    }
  }
}

class ChordsMethod
    : public Method<
          std::function<double(Equation const &, double, double, double)>> {
public:
  ChordsMethod() : Method(chords_function, "Метод хорд") {}
  double solve(Equation const &eq, double a, double b,
               double accuracy) const override {
    return get_solver()(eq, a, b, accuracy);
  }
};

#endif /* DE2924C4_073E_48DC_974A_BC468B152882 */
